﻿/**
 * 2016.08.29
 * 项目描述文件
 */

//图片tinify压缩
const USE_TINIFY = false;

const DEBUG = isDev();
const PRODUCE = !DEBUG;

const CDN_PRE = '';
//const CDN_PRE = '//static.chidaoni.com';


const SOURCCE = '*.{js,css,jpg,png,gif,vue,map,ico,eot,ttf}';
const DIR = '/FE-V-NEXT';


outputMessage();


//设置当前编译文件//设置入口文件即可
fis.set('project.files', [
    '*.cshtml',
    '*.html',
]);
/*fis.set('project.ignore', [
    '/Views/**'
]);*/

//http://fis.baidu.com/fis3/docs/api/config-props.html
//设置MD5后缀的长度
fis.set('project.md5Length', 8);

//设置MD5后缀的连接字符
//fis.set('project.md5Connector ', '.');                

//启用amd
fis.hook('amd', {
    extList: ['.js', '.vue'],
    // 配置项
    paths: {
        require: '/node_modules/requirejs/require.js',
        vue: '/node_modules/vue/dist/vue.common.js',
    },
    shim: {}
});
fis.unhook('components');
fis.hook('node_modules');









///////////////////
///javascript模块化/编译
//////////////////
fis.match('*.vue', {
    rExt: 'js',
    isMod: true,
    parser: [
        fis.plugin('vue-component', {
            cssScopedFlag: '__vuec__',
            cssScopedIdPrefix: '_v-',
            cssScopedHashType: 'sum',
            cssScopedHashLength: 8,
            styleNameJoin: ''
        })
    ],
}).match('*.{js,vue}', {
    isMod: true,
    useHash: PRODUCE,
    optimizer: PRODUCE && fis.plugin('uglify-js'),
}).match('components/**.js', {
}).match('/node_modules/**.js', {
    optimizer: fis.plugin('uglify-js'),
}).match('/node_modules/requirejs/require.js', { //去除加载器的define
    isMod: false
}).match('*.{js,vue:js}', {
    preprocessor: [
        fis.plugin('js-require-css'),
        fis.plugin('js-require-file', {
            useEmbedWhenSizeLessThan: 10 * 1024 // 小于10k用base64
        })
    ],
    parser: [
        fis.plugin('typescript', {
            //target: 0
        })
    ]
});








///////////////////
///打包--依赖分析
//////////////////
fis.match('::package', {
    //https://github.com/fex-team/fis3-postpackager-loader
    postpackager: fis.plugin('loader', {
        //useInlineMap: true,
        sourceMap: DEBUG,
        useTrack: DEBUG,
        includeAsyncs: true, //包含异步模块
        allInOne: {
            js: function(file) {
                return `${DIR}/${file.subdirname}/${file.filename}.js`;
            },
            css: function(file) {
                return `${DIR}/${file.subdirname}/${file.filename}.css`;
            }
        }
    }),
    //https://github.com/fex-team/fis3-packager-deps-pack
    packager: fis.plugin('deps-pack', {
        useSourceMap: DEBUG,
        useTrack: DEBUG,
        [`${DIR}/js/pkg-p.js`]: [
            '/node_modules/requirejs/require.js',
            '/node_modules/vue/dist/vue.common.js',
        ],
    })
});









///////////////////
///css处理+预处理
//////////////////
fis.match('*.{scss,vue:scss}', {
    parser: fis.plugin('node-sass'),
    rExt: 'css'
}).match('*.css', {
    useHash: PRODUCE,
    optimizer: PRODUCE && fis.plugin('clean-css'),
    postprocessor: fis.plugin("autoprefixer", {
        // https://github.com/ai/browserslist#queries
        "browsers": ['Firefox >= 20', 'Safari >= 6', 'Explorer >= 9', 'Chrome >= 12', "ChromeAndroid >= 4.0"],
        "flexboxfixer": true,
        "gradientfixer": true
    }),
});









///////////////////
///图片
//////////////////
fis.match('::image', {
    useHash: PRODUCE,
    //https://github.com/zswang/fis-optimizer-tinify
    optimizer: PRODUCE && USE_TINIFY && fis.plugin('tinify', {
        key: ['hNMHKdlaHTaUne8Qq3zADVJeEB_SuS5Z'],
        cacheDir: __dirname + '/.cache'
    }),
});









///////////////////
///增加html的__uri函数
//////////////////
fis.match('*.{cshtml,html}', {
    parser: fis.plugin('html-uri'),
    //optimizer: fis.plugin('html-minifier'), //出错
    //optimizer: fis.plugin('htmlmin'), //出错
});





//------------------------------------------------扫尾工作-----
fis.match('**/favicon.ico', {
    useHash: false
});

///////////////////
///默认编译方式
//////////////////
fis.match('*', {
    deploy: [
        fis.plugin('skip-packed', {
            ignore: []
        }),
        fis.plugin('local-deliver', {
            //to: '../'
        })
    ]
}).match(SOURCCE, {
    url: CDN_PRE + '$0',
    deploy: [
        fis.plugin('skip-packed', {
            ignore: []
        }),
        fis.plugin('local-deliver', {
            //to: '../Static'
        })
    ]
});





function outputMessage() {
    var dev = isDev();
    //输出F.I.S
    fis.cli.version();
    console.log('当前F.I.S编译模式::', process.env.NODE_ENV);
}

function isDev() {
    return 'production' !== process.env.NODE_ENV;
}